import { _decorator, Component, Node } from 'cc';
import { Logger } from 'db://assets/util';
import {
    MajorContainer,
    MinorContainer,
    NarratorContainer,
} from '../container/dialog';
import { ServiceLocator } from '../service';
import { Chapter } from 'db://assets/store';
import { STATE } from 'db://assets/enum';
import { Printer } from './Printer';
const { ccclass, property, menu } = _decorator;

enum Role {
    Major,
    Minor,
    Narrator,
}

@ccclass('DialogFrame')
@menu('frame/Dialog')
export class DialogFrame extends Component {
    #logger = new Logger('对话框架::DialogFrame')
    #services = ServiceLocator;

    @property(MajorContainer)
    majorContainer: MajorContainer

    @property(MinorContainer)
    minorContainer: MinorContainer

    @property(NarratorContainer)
    narratorContainer: NarratorContainer

    get chapter(): Chapter {
        return this.#services.get<Chapter>(STATE.CHAPTER)
    }

    start() {
        this.#logger.debug("#start")
    }

    async clear(target = [Role.Major, Role.Minor, Role.Narrator]) {
        this.#logger.debug('#clear')
        if (target.includes(Role.Major) && this.majorContainer.enabled) {
            await this.majorContainer.clear()
        }
        if (target.includes(Role.Minor) && this.minorContainer.enabled) {
            await this.minorContainer.clear()
        }
        if (target.includes(Role.Narrator) && this.narratorContainer.enabled) {
            await this.narratorContainer.clear()
        }
    }

    async showMajorDialog(dialogContent: LDContent.DialogContent) {
        this.#logger.debug('显示主对话::@showMajorDialog', dialogContent)
        await this.clear([Role.Minor, Role.Narrator])
        this.majorContainer.context = dialogContent.dialog.what.text
        this.majorContainer.node.active = true
    }

    async showMinorDialog(dialogContent: LDContent.DialogContent) {
        this.#logger.debug('显示配角对话::@showMinorDialog', dialogContent)
        await this.clear([Role.Major, Role.Narrator])
        const role = this.chapter.getRole(dialogContent.dialog.who.roleId)
        this.#logger.debug('角色', role)
        this.minorContainer.nickName = role.nickname
        this.minorContainer.context = dialogContent.dialog.what.text
        this.minorContainer.node.active = true
    }

    async showNarratorDialog(dialogContent: LDContent.DialogContent) {
        this.#logger.debug('显示旁白对话::@showNarratorDialog', dialogContent)
        await this.clear([Role.Major, Role.Minor])

        this.narratorContainer.context = dialogContent.dialog.what.text

        this.narratorContainer.node.active = true
    }

}

